package com.lzk.es;

import com.lzk.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

/**
 * 加强练习
 */
public class EsTest {
    /**
     * 添加一个文档
     */
    @Test
    public void testAdd(){
        //1.获取es对象连接
        TransportClient client = ESClientUtil.getClient();
        //2.创建索引
        IndexRequestBuilder index = client.prepareIndex("people", "user", "2");
        //3.准备一个文档数据(一条数据)
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",1);
        map.put("name","蜘蛛侠");
        map.put("age",24);
        map.put("sex",1);
        //4.获取结果
        IndexResponse response = index.setSource(map).get();
        System.out.println(response);
        //5.关闭连接
        client.close();
    }

    /**
     * 查询根据文档id查询
     */
    @Test
    public void testGet(){
        //1.获取es对象连接
        TransportClient client = ESClientUtil.getClient();
        //2.查询
        GetRequestBuilder builder = client.prepareGet("people", "user", "2");
        System.out.println(builder.get());
        System.out.println(builder.get().getSource());
        //3.关流
        client.close();
    }

    /**
     * 根据文档ID更新文档
     */
    @Test
    public void testUpdate(){
        //1.获取es对象连接
        TransportClient client = ESClientUtil.getClient();
        //2.修改索引
        UpdateRequestBuilder builder = client.prepareUpdate("people", "user", "2");
        //3.准备修改数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",2);
        map.put("name","蝙蝠侠");
        map.put("age",30);
        map.put("sex",1);
        //4.设置修改内容返回结果
        UpdateResponse response = builder.setDoc(map).get();
        System.out.println(response);
        //5.关流
        client.close();

    }

    /**
     * 根据文档ID删除文档
     */
    @Test
    public void testDel(){
        //1.获取ES对象连接
        TransportClient client = ESClientUtil.getClient();
        //2.删除
        DeleteRequestBuilder builder = client.prepareDelete("people", "user", "2");
        System.out.println(builder.get());
        //3.关流
        client.close();
    }

    /**
     * 批量添加文档
     */
    @Test
    public void testBatch() throws Exception {
        //获取es连接对象
        TransportClient client = ESClientUtil.getClient();
        //创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        for (int i = 0; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("people", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i % 2)
                            .endObject()
            );
            BulkResponse response = bulk.add(builder).get();
            System.out.println(response.status());
        }
        // 关闭连接
        client.close();
    }

    /**
     * - 查询用户表
     * - name包含：我在源码
     * - age在1~12之间
     * - sex=1
     * - 每页大小2
     * - 从2页开始查
     * - 按照age倒序
     */
    @Test
    public void testDemo(){
        //1.获取es对象连接
        TransportClient client = ESClientUtil.getClient();

        SearchRequestBuilder people = client.prepareSearch("people");
        //查询条件
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        List<QueryBuilder> must = boolQuery.must();
        must.add(QueryBuilders.matchQuery("name","我在源码"));

        boolQuery.filter(QueryBuilders.termQuery("sex",1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));

        //每页大小2
        people.setSize(2);
        //从2页开始查
        people.setFrom(2);
        //按照age倒序
        people.addSort("age", SortOrder.DESC);

        //放入设置条件
        people.setQuery(boolQuery);

        SearchResponse response = people.get();
        SearchHits hits = response.getHits();
        System.out.println("共查询条数:"+hits.getTotalHits());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

        //关流
        client.close();
    }
}
